﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ImbaTracker.Models;
using System.Data.Objects;
using System.Security.Principal;


namespace ImbaTracker.Content {
    public class Pair{
        public int First, Second;
        public Pair(int first, int second) {
            First = first;
            Second = second;
        }
    }

    public static class Helper {
        private static T selectOne<T>(IQueryable<T> all, T defaultValue) {
            if (all.Count() == 0) {
                return defaultValue;
            } else {
                return all.Single();
            }
        }

        public static Project getProject(int projectId) {
            var all = (from p in (new ImbaDBEntities()).Projects where p.Id == projectId select p);
            return selectOne(all, null);
        }

        public static User getUser(int userId) {
            return (from p in (new ImbaDBEntities()).Users where p.Id == userId select p).FirstOrDefault();
        }

        public static string getUserName(int? userId) {
            var userIdNew = userId == null ? 0 : userId.Value;
            var user = getUser(userIdNew);
            return user == null ? "" : user.Name;
        }

        public static Status getStatus(int statusId) {
            return (from p in (new ImbaDBEntities()).Status where p.Id == statusId select p).FirstOrDefault();
        }

        public static int curUserId(IPrincipal user) {
            return (from i in (new ImbaDBEntities()).Users where i.Login == user.Identity.Name select i.Id).Single();
        }

        public static bool canAccess(IPrincipal user, int userId = 0) {
            return user.IsInRole("Administrator") || curUserId(user) == userId;
        }

        public static bool canProjectAccess(IPrincipal user, int projectId = 0) {
            return user.IsInRole("Administrator") || isProjectOwner(curUserId(user), projectId);
        }

        public static List<User> getOwners(int projectId) {
            var project = getProject(projectId);
            if (project == null) {
                return new List<User>();
            } else {
                return project.Owners.ToList();
            }
        }

        public static List<Project> projectsToView(IPrincipal user, bool onlyMy = false) {
            var curUser = curUserId(user);
            List<Project> res = new List<Project>();
            foreach (var p in (new ImbaDBEntities()).Projects) {
                if (!onlyMy && (!p.Hidden || user.IsInRole("Administrator"))) {
                    res.Add(p);
                    continue;
                }
                foreach (var u in p.Members) {
                    if (u.Id == curUser) {
                        res.Add(p);
                        break;
                    }
                }
            }
            return res;
        }

        public static List<User> getMembers(int projectId) {
            var project = getProject(projectId);
            if (project == null) {
                return new List<User>();
            } else {
                return (from u in project.Members select u).ToList();
            }
        }

        public static bool isProjectOwner(int userId, int projectId) {
            foreach (User user in getOwners(projectId)) {
                if (userId == user.Id) {
                    return true;
                }
            }
            return false;
        }

        public static bool isProjectMember(int userId, int projectId) {
            foreach (User user in getMembers(projectId)) {
                if (userId == user.Id) {
                    return true;
                }
            }
            return false;
        }
    }
}